package com.service.impl;

import java.util.HashMap;
import java.util.List;

import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bean.VideoContext;
import com.dao.VideoContextDao;
import com.service.VideoContextService;
import com.util.CacheUtil;
import com.util.LoggerUtil;
import com.util.Pager;
import com.util.Redis;
@Service
public class VideoContextServiceImpl implements VideoContextService{
	public static Logger logger=Logger.getLogger(VideoContextServiceImpl.class);
	@Autowired
	private VideoContextDao videoContextDao;
	
	
	@Override
	public Map<String, Object> selectListByVideo(Integer curPage,
			Integer pageSize, String areaId,String tag,JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			String keys=logs.getString("keys");
			Pager<VideoContext> pager=videoContextDao.selectListByVideo(curPage, pageSize,areaId,tag);
			if(pager!=null&&pager.getData().size()>0){
				map.put("result", "success");
				map.put("rows", pager);
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				LoggerUtil.backlogsFalseNOData(logs, logger);
				
			}
			if(StringUtils.isNotEmpty(keys)){
				Redis.setRedisData(keys, CacheUtil.getValue(map));
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		
		return map;
	}
	@Override
	public Map<String, Object> selectFollowVideo(Integer curPage,
			Integer pageSize, String id,JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			Pager<VideoContext> pager=videoContextDao.selectFollowVideo(curPage, pageSize,id);
			if(pager!=null&&pager.getData().size()>0){
				map.put("result", "success");
				map.put("rows", pager);
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				LoggerUtil.backlogsFalseNOData(logs, logger);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		
		return map;
	}
	@Override
	public Map<String, Object> selectByVideo(Integer curPage, Integer pageSize,
			String tag,JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			String keys=logs.getString("keys");
			Pager<VideoContext> pager=videoContextDao.selectByVideo(curPage,pageSize,tag);
			if(pager!=null&&pager.getData().size()>0){
				map.put("result", "success");
				map.put("rows", pager);
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				LoggerUtil.backlogsFalseNOData(logs, logger);
			}
			if(StringUtils.isNotEmpty(keys)){
				Redis.setRedisData(keys, CacheUtil.getValue(map));
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		
		return map;
	}
	@Override
	public Map<String, Object> selectOfficialListVideo(Integer curPage,
			Integer pageSize, String userId,JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			Pager<VideoContext> pager=videoContextDao.selectOfficialListVideo(curPage,pageSize,userId);
			if(pager!=null&&pager.getData().size()>0){
				map.put("result", "success");
				map.put("rows", pager);
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				LoggerUtil.backlogsFalseNOData(logs, logger);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		
		return map;
	}
	@Override
	public Map<String, Object> selectByVideoZan(Integer curPage,
			Integer pageSize,JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			String keys=logs.getString("keys");
			Pager<VideoContext> pager=videoContextDao.selectByVideoZan(curPage,pageSize);
			if(pager!=null&&pager.getData().size()>0){
				map.put("result", "success");
				map.put("rows", pager);
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				LoggerUtil.backlogsFalseNOData(logs, logger);
			}
			if(StringUtils.isNotEmpty(keys)){
				Redis.setRedisData(keys, CacheUtil.getValue(map));
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		return map;
	}
	@Override
	public Map<String, Object> updateZanNum(String id, String user_id) {
		return videoContextDao.updateZanNum(id, user_id);
	}
	@Override
	public Map<String, Object> findById(String id,String userId,JSONObject logs) {
		return videoContextDao.findById(id,userId,logs);
	}
	@Override
	public Map<String, Object> selectByFindRelate(String videoId, JSONObject logs) {
		Map<String, Object> map=new HashMap<String, Object>();
		try {
			List<VideoContext> data=videoContextDao.selectByFindRelate(videoId);
			if(data!=null&&data.size()>0){
				map.put("result", "success");
				map.put("rows", data);
				LoggerUtil.backlogsSuccess(logs, logger);
			}else{
				map.put("result", "false");
				LoggerUtil.backlogsFalseNOData(logs, logger);
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "101");
			LoggerUtil.backlogsFalseError(logs, logger,e);
			return map;
		}
		return map;
	}
}
